const { resolve } = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const tsImportPluginFactory = require('ts-import-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
  mode: 'production',
  context: resolve(__dirname, 'src'),
  entry: './index.tsx',
  output: {
    path: resolve(__dirname, 'build/prod'),
    publicPath: '/course/',
    filename: 'bundle-[hash:8].js',
  },
  resolve: {
    alias: {
      pages: resolve(__dirname, 'src/pages'),
      models: resolve(__dirname, 'src/models'),
      components: resolve(__dirname, 'src/components'),
      actions: resolve(__dirname, 'src/actions'),
      reducers: resolve(__dirname, 'src/reducers'),
      api: resolve(__dirname, 'src/api'),
      constants: resolve(__dirname, 'src/constants'),
      utils: resolve(__dirname, 'src/utils'),
      assets: resolve(__dirname, 'src/assets'),
      containers: resolve(__dirname, 'src/containers'),
      config: resolve(__dirname, 'src/config'),
      store: resolve(__dirname, 'src/store'),
    },
    extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
  },
  module: {
    rules: [
      {
        test: /\.(ts|tsx)?$/,
        use: [
          {
            loader: 'ts-loader',
            options: {
              transpileOnly: true,
              getCustomTransformers: () => ({
                before: [
                  tsImportPluginFactory({
                    libraryName: 'antd',
                    libraryDirectory: 'es',
                    style: 'css',
                  }),
                ],
              }),
              compilerOptions: {
                module: 'es2015',
              },
            },
          },
        ],
        exclude: [resolve(__dirname, 'node_modules')],
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader'],
      },
      {
        test: /\.less$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'less-loader'],
      },
      {
        test: /\.scss|\.sass$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
      },
      { test: /\.png$/, loader: 'url-loader?limit=100000' },
      { test: /\.jpg$/, loader: 'file-loader' },
      {
        test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
        loader: 'url-loader?limit=10000&mimetype=application/font-woff',
      },
      {
        test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
        loader: 'url-loader?limit=10000&mimetype=application/octet-stream',
      },
      { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file-loader' },
      {
        test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
        loader: 'url-loader?limit=10000&mimetype=image/svg+xml',
      },
    ],
  },
  plugins: [
    new CleanWebpackPlugin(['build/prod']),
    new MiniCssExtractPlugin({
      filename: '[name].[chunkhash:8].css',
      chunkFilename: '[id].css',
    }),
    new webpack.NamedModulesPlugin(),

    new HtmlWebpackPlugin({ template: resolve(__dirname, 'src/index.html') }),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"',
      },
    }),
  ],
};
